package com.cn.web.handler;

import cn.com.allunion.common.AllUnionResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

//@ControllerAdvice
public class RestResponseEntityExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestResponseEntityExceptionHandler.class);

    @ExceptionHandler
    protected ResponseEntity<String> handleInternalException(Exception ex) {
        LOGGER.error(ex.getMessage(), ex);
        return new ResponseEntity("Requested resource does not found", HttpStatus.NOT_FOUND);
    }

    /**
     * 处理访问接口，由于没有携带指定的参数，抛出的异常
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        AllUnionResult result = AllUnionResult.build(status.value(), "参数错误");
        return new ResponseEntity<Object>(result, headers, status);
    }
}